package net.sudot.chess.task;

import net.sudot.chess.business.model.OrderRecharge;
import net.sudot.chess.business.service.OrderRechargeService;
import net.sudot.commons.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单任务
 *
 * @author tangjialin on 2018-05-18.
 */
@Component
public class OrderTask {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private OrderRechargeService orderRechargeService;

    /**
     * 订单过期处理
     */
    @Lock(name = "lock:scheduled:order:expired:processing")
    @Scheduled(cron = "${scheduled.order.expired.processing.cron}")
    public void expiredProcessing() {
        List<OrderRecharge> orderRechargeList = orderRechargeService.findListExpired();
        for (OrderRecharge order : orderRechargeList) {
            try {
                orderRechargeService.cancel(order);
            } catch (Exception e) {
                logger.error("充值订单过期处理异常", e);
            }
        }
    }

}
